<template>
  <e-table
    @getList="handleGetList"
    :tableData="tableData"
    :columns="columns"
    :loading="loading"
    :total="total"
    @row-click="handleRowClick"
    :pageObj="null"
  >
    <el-table-column
      slot="order"
      label="序号"
      type="index"
      :index="indexMethod"
      width="60"
      align="center"
    />
    <el-table-column slot="sort" label="排序" width="100" align="center">
      <template slot-scope="scope">
        <el-input
          style="width: 65px"
          :max="100"
          size="mini"
          v-model="scope.row.dictionariesOrder"
          @blur="handleOrderChange(scope.row)"
        />
      </template>
    </el-table-column>
    <el-table-column label="是否末级" align="center" width="80" slot="leaf">
      <template slot-scope="scope">
        <span>{{ scope.row.leaf ? '是' : '否'}}</span>
      </template>
    </el-table-column>
    <el-table-column slot="state" width="80" align="center" label="状态">
      <template slot-scope="scope">
        <el-tag type="success" v-if="scope.row.state === 0">正常</el-tag>
        <el-tag type="danger" v-else>禁用</el-tag>
      </template>
    </el-table-column>
    <el-table-column slot="action" fixed="right" width="50" align="center">
      <template slot-scope="scope">
        <el-dropdown>
          <a class="el-dropdown-link">
            <i class="el-icon-menu"></i>
          </a>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item @click.native="handleEdit(scope.row)">
              <a>编辑</a>
            </el-dropdown-item>
            <el-dropdown-item v-if="scope.row.allowDel" @click.native="handleDelete(scope.row)">
              <a class="red">删除</a>
            </el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </template>
    </el-table-column>
  </e-table>
</template>

<script>
//
import { getDicTable, deleteDic } from '@/api/dictionary'
import { openNotification, orderChange } from '@/utils/util'

export default {
  data() {
    return {
      loading: false,
      // 查询参数
      queryParam: {
        id: '',
        pageSize: 10,
        pageNo: 1
      },
      total: 1,
      columns: [
        {
          slot: 'order'
        },
        {
          label: '字典名称',
          prop: 'dictionariesShowName'
        },
        {
          label: '编码',
          prop: 'dictionariesId'
        },
        {
          slot: 'leaf'
        },
        {
          label: '说明',
          prop: 'remark'
        },
        {
          slot: 'sort'
        },
        {
          slot: 'action'
        }
      ],
      // 表头
      tableData: [],
      selectObj: {}
    }
  },
  mounted() {
    this.handleGetList()
  },
  methods: {
    indexMethod(index) {
      return (this.queryParam.pageNo - 1) * this.queryParam.pageSize + index + 1
    },
    handleRowClick(row, column, event) {
      let obj = {}
      obj.parentName = row.dictionariesShowName
      obj.parentId = row.dictionariesId
      this.$emit('setTree', obj)
    },
    getId(obj) {
      console.log('obj', obj)
      this.$set(this.queryParam, 'id', obj.parentId)
      this.queryParam.pageNo = 1
      this.selectObj = obj
      this.handleGetList()
    },
    async handleOrderChange(row) {
      const res = await orderChange(row, 'dictionariesOrder', 'dictionariesId', 'dictionaries')
      if (res.state === '000000') {
        this.handleGetList()
      }
    },
    handleEdit(record) {
      const { parentName, parentId } = this.selectObj
      this.$nextTick(() => {
        const obj = Object.assign(record, { parentName, parentId })
        this.$emit('edit', obj)
      })
    },
    handleDelete(row) {
      const _this = this
      this.$confirm({
        title: '删除操作',
        content: `您确定删除[${row.dictionariesShowName}]这条记录吗？`,
        okText: '确定',
        okType: 'danger',
        cancelText: '取消',
        centered: true,
        async onOk() {
          // 调用删除接口，刷新表格数据
          const res = await deleteDic(row.dictionariesId)
          openNotification('success', '删除成功')
          _this.handleGetList()
          _this.$emit('refresh')
        }
      })
    },
    async handleGetList(param = {}) {
      this.loading = true
      this.queryParam = Object.assign(this.queryParam, param)
      const res = await getDicTable(this.queryParam)
      if (res.data && res.data.length > 0) {
        const { state, data } = res
        this.tableData = data
      } else {
        this.tableData = []
      }
      this.loading = false
    }
  }
}
</script>
